﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraBars;
using System.Xml;
using System.Reflection;
using System.Windows.Forms;

namespace Client
{
   public class MenuBuilder
    {
       public static void GetMenu(BarManager manager,Bar bar, string fileName)
       {
           manager.BeginInit();
           try
           {
               //XmlDocument doc = new XmlDocument();
               XmlDocument doc = new SporeXML();//使用加密文件
               doc.Load(fileName);
               foreach (XmlNode v in doc.DocumentElement.ChildNodes)
               {
                   BarItem temp = GetBarItemFromXmlNode(manager, v);
                   manager.Items.Add(temp);
                   bar.LinksPersistInfo.Add(new LinkPersistInfo(temp));
               }
           }
           catch (Exception ex)
           {
               QunYing.Common.Loger.Log.Error("加载菜单时发生异常", ex);
               throw ex;
           }
           finally
           {
               manager.EndInit();
           }
       }

       private static BarItem GetBarItemFromXmlNode(BarManager manager,XmlNode node)
       {
           if (node.HasChildNodes)
           {
               BarSubItem item = new BarSubItem();
               item.Caption = node.Attributes["text"].Value;
               item.Id = Int32.Parse(node.Attributes["id"].Value);
               item.Name = node.Attributes["name"].Value;
               item.Visibility = node.Attributes["needright"].Value.Contains(GlobalInfo.UserLoginInfo.角色)? BarItemVisibility.Always:BarItemVisibility.Never;
               foreach (XmlNode childNode in node.ChildNodes)
               {
                   BarItem temp = GetBarItemFromXmlNode(manager,childNode);
                   manager.Items.Add(temp);
                   item.LinksPersistInfo.Add(new LinkPersistInfo(temp, childNode.Attributes["beginGroup"].Value == "1"));
               }
               return item;
           }
           else
           {
               BarButtonItem item = new BarButtonItem();
               item.Caption = node.Attributes["text"].Value;
               item.Id = Int32.Parse(node.Attributes["id"].Value);
               item.Name = node.Attributes["name"].Value;
               item.Visibility = node.Attributes["needright"].Value.Contains(GlobalInfo.UserLoginInfo.角色) ? BarItemVisibility.Always : BarItemVisibility.Never;
               if (GlobalInfo.IsEmployee)
               {
                   if (node.Attributes["imageName"].Value != "1")
                   {
                       item.Visibility = BarItemVisibility.Never;
                   }
               }
               if (!string.IsNullOrEmpty(node.Attributes["typeName"].Value))
               {
                   item.ItemClick += (sender, e) =>
                   {
                       string rootPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                       Assembly asmb = Assembly.LoadFrom(rootPath + node.Attributes["dllFileName"].Value);
                       Type type = asmb.GetType(node.Attributes["typeName"].Value);
                       Form form = SingleObjManager.GetSingleObj(type) as Form;
                       if (form is BaseChildForm)
                       {
                           form.Show();
                           GlobalInfo.MainForm.ActivedChildForm(form);
                       }
                       else
                       {
                           form.ShowDialog();
                       }
                   };
               }
               return item;
           }

           
       }
    }
}
